/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.toolkit;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.util.Vector;

class RootMouseHandler implements MouseListener, MouseMotionListener,
MouseWheelListener {
	private RootPane root;
	private Vector<CComponent> hierarchy = new Vector<CComponent>();
	private CComponent grabComponent;
	private int grabButton;
	
	RootMouseHandler(RootPane root) {
		this.root = root;
	}
	
	private void updateHierarchy(Vector<CComponent> nh, MouseEvent e) {
		int i;
		Point2D.Double p = new Point2D.Double(e.getX(), e.getY());
		Point2D.Double p2 = new Point2D.Double();
		
		for(i = 0; i < Math.min(hierarchy.size(), nh.size()); i++)
			if(hierarchy.elementAt(i) != nh.elementAt(i))
				break;
		
		for(int j = hierarchy.size() - 1; j >= i; j--)
		{
			CComponent c = hierarchy.elementAt(j);
			Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
			
			try {
				c.getTotalTransform().createInverse().transform(p, p2);
			} catch (NoninvertibleTransformException e1) {
			}
			
			scenic.toolkit.event.MouseEvent te = 
				new scenic.toolkit.event.MouseEvent(c, p2, 0, e.getButton());
			
			for(scenic.toolkit.event.MouseListener l : listeners)
				l.mouseExited(te);
		}
		
		for(int j = i; j < nh.size(); j++)
		{
			CComponent c = nh.elementAt(j);
			Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
			
			try {
				c.getTotalTransform().createInverse().transform(p, p2);
			} catch (NoninvertibleTransformException e1) {
			}
			
			scenic.toolkit.event.MouseEvent te = 
				new scenic.toolkit.event.MouseEvent(c, p2, 0, e.getButton());
			
			for(scenic.toolkit.event.MouseListener l : listeners)
				l.mouseEntered(te);
		}
		
		hierarchy = nh;
	}
	
	private Vector<CComponent> getHierarchy(Point2D p) {
		Vector<CComponent> h = new Vector<CComponent>();		
		CComponent c = root.getContent();
		CComponent child;
		
		h.add(c);
		try {
			while((child = c.getChildAt(p)) != null) {
				c = child;
				p = c.getTransform().createInverse().transform(p, null);
				h.add(c);
			}
		} catch (NoninvertibleTransformException e) {
		}
		
		return h;
	}
	
	public void mouseDragged(MouseEvent e) {
		if(grabComponent == null)
			return;
		Point2D p = new Point2D.Double(e.getX(), e.getY());
		Vector<CComponent> h;
		scenic.toolkit.event.MouseEvent scenicEvent;	
		
		try {
			p = grabComponent.getTotalTransform().inverseTransform(p, null);
		} catch (NoninvertibleTransformException e1) {
			return;
		}
		
		scenicEvent = new scenic.toolkit.event.MouseEvent(
				grabComponent, 
				p, 
				0, 
				e.getButton());
		
		Vector<scenic.toolkit.event.MouseMotionListener> listeners = grabComponent.getMouseMotionListeners();
		
		for(scenic.toolkit.event.MouseMotionListener l : listeners) {
			l.mouseDragged(scenicEvent);
		}
	}

	public void mouseMoved(MouseEvent e) {
		Point2D.Double p = new Point2D.Double(e.getX(), e.getY());
		Vector<CComponent> h;
		scenic.toolkit.event.MouseEvent scenicEvent;	
		CComponent component;
		
		h = getHierarchy(p);
		updateHierarchy(h, e);
		
		component = getComponentAt(p);
		
		scenicEvent = new scenic.toolkit.event.MouseEvent(
				component, 
				p, 
				0, 
				e.getButton());
		
		Vector<scenic.toolkit.event.MouseMotionListener> listeners = component.getMouseMotionListeners();
		
		for(scenic.toolkit.event.MouseMotionListener l : listeners) {
			l.mouseMoved(scenicEvent);
		}
	}

	private static final int MOUSE_CLICKED = 0;
	private static final int MOUSE_PRESSED = 1;
	private static final int MOUSE_RELEASED = 2;
	private static final int MOUSE_WHEEL_MOVED = 3;
	
	private CComponent getComponentAt(Point2D.Double point) {
		Point2D p = point;
		CComponent component = root.getContent();
		CComponent child;
		
		try {
			while((child = component.getChildAt(p)) != null) {
				component = child;
				p = component.getTransform().createInverse().transform(p, null);
			}
		} catch (NoninvertibleTransformException e1) {
		}
		
		point.x = p.getX();
		point.y = p.getY();
		
		return component;
	}
	
	public boolean sendButtonEvent(MouseEvent e, int type, CComponent component) {
		Vector<scenic.toolkit.event.MouseListener> listeners = component.getMouseListeners();
		scenic.toolkit.event.MouseEvent te;
		Point2D p = new Point2D.Double(e.getX(), e.getY());
		Point2D p2;
		
		if(listeners.isEmpty())
			return false;
		
		try {
			p2 = component.getTotalTransform().inverseTransform(p, null);
		} catch(NoninvertibleTransformException e1) {
			return false;
		}
		
		if(type == MOUSE_WHEEL_MOVED) {
			MouseWheelEvent we = (MouseWheelEvent)e;
			
			te = new scenic.toolkit.event.MouseWheelEvent(
					component, 
					p2, 
					0, 
					e.getButton(),
					we.getUnitsToScroll() * 30.0);
		} else {
			te = new scenic.toolkit.event.MouseEvent(
					component, 
					p2, 
					0, 
					e.getButton());
		}
		
		for(scenic.toolkit.event.MouseListener l : listeners) {
			switch(type) {
			case MOUSE_CLICKED: l.mouseClicked(te); break;
			case MOUSE_PRESSED: l.mousePressed(te); break;
			case MOUSE_RELEASED: l.mouseReleased(te); break;
			case MOUSE_WHEEL_MOVED: l.mouseWheelMoved((scenic.toolkit.event.MouseWheelEvent)te); break;
			}
		}
		if(te.isConsumed()) {
			if(type == MOUSE_PRESSED) {
				if(grabComponent == null) {
					grabComponent = component;
					grabButton = e.getButton();
				}
			}
			return true;
		}		
		return false;
	}
	
	public void handleButtonEvent(MouseEvent e, int type) {
		Point2D p = new Point2D.Double(e.getX(), e.getY());
		Vector<CComponent> hierarchy = getHierarchy(p);
		
		if(type == MOUSE_RELEASED) {
			if(grabComponent != null && e.getButton() == grabButton) {
				sendButtonEvent(e, type, grabComponent);
				grabComponent = null;
				grabButton = 0;
			}
		}
		
		for(int i = hierarchy.size() - 1; i >= 0; i--) {
			sendButtonEvent(e, type, hierarchy.elementAt(i));
		}
	}
	
	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mouseClicked(MouseEvent e) {
		handleButtonEvent(e, MOUSE_CLICKED);
	}

	public void mousePressed(MouseEvent e) {
		handleButtonEvent(e, MOUSE_PRESSED);
	}

	public void mouseReleased(MouseEvent e) {
		handleButtonEvent(e, MOUSE_RELEASED);
	}

	public void mouseWheelMoved(MouseWheelEvent e) {
		handleButtonEvent(e, MOUSE_WHEEL_MOVED);
	}
}
